﻿using System;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;
using Yglu.BusinessLayer.Reports;

namespace Yglu.WebAdmin
{
	public partial class Promotions_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			CheckPermissions(AdminLevel.Store);

			if (!IsPostBack)
			{
				PanEdit.Visible = false;

				SetDefaults();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			if (PanGrid.Visible)
			{
				LoadData();
			}

			PlaceReports.Visible = PanEdit.Visible && TxtPromotionId.Text != "0";
		}

		private void SetDefaults()
		{
			SetDefaults(GridPromotions);
			SetDefaults(GridReports);

			TxtPromotionId.Enabled = false;

			if (SessionManager.LoggedUser.AdminLevel >= AdminLevel.Company)
			{
				CboStore.DataSource = Settings.Stores;
				CboStore.DataBind();

				CboStore.Items.Insert(0, new ListItem(Messages.AllStores, "0"));
				CboStore.SelectedValue = SessionManager.LoggedStore.Id.ToString();
			}
			else
			{
				ButAddNew.Visible = false;
				CboStore.Enabled = false;

				CboStore.Items.Add(new ListItem(SessionManager.LoggedStore.Title, SessionManager.LoggedStore.Id.ToString()));
			}
		}

		private void LoadData()
		{
			if (SessionManager.LoggedStore.Id > 0)
			{
				GridPromotions.DataSource = new PromotionCollection(DataAccess.Promotion_Get(SessionManager.LoggedStore));
			}
			else
			{
				GridPromotions.DataSource = new PromotionCollection(DataAccess.Promotion_Get());
			}
			
			GridPromotions.DataBind();

			LblGridCount.Text = GetLocalResourceObject("LblGridCount.Text").ToString().Replace("#", GridPromotions.Rows.Count.ToString());

			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion

		#region Add and edit

		protected void ButAddNew_Click(Object sender, EventArgs e)
		{
			TxtPromotionId.Text = "0";
			TxtPromotionTitle.Text = String.Empty;
			TxtPromotionDiscount.Text = String.Empty;
			ChkPromotionIsPercent.Checked = false;
			ChkPromotionAutomatic.Checked = true;
			ChkPromotionActive.Checked = true;

			LitEditLegend.Text = GetLocalResourceObject("LitAddPromotion.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		protected void GridPromotions_RowCommand(Object sender, GridViewCommandEventArgs e)
		{
			EditPromotion(Convert.ToInt32(e.CommandArgument));
		}

		private void EditPromotion(Int32 row)
		{
			Int32 id = Convert.ToInt32(GridPromotions.Rows[row].Cells[0].Text);
			Promotion p = new Promotion(id);

			TxtPromotionId.Text = p.Id.ToString();
			TxtPromotionTitle.Text = p.Title;
			TxtPromotionDiscount.Text = p.Discount.ToString("0.00");
			TxtPromotionProducts.Text = p.Products.ToString();
			TxtPromotionCombos.Text = p.Combos.ToString();
			ChkPromotionIsPercent.Checked = p.IsPercent;
			ChkPromotionAutomatic.Checked = p.Automatic;
			ChkPromotionActive.Checked = p.Active;
			CboStore.SelectedValue = p.StoreId.ToString();

            LitEditLegend.Text = GetLocalResourceObject("LitEditPromotion.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;

			LoadReports(p);
		}

		private void LoadReports(Promotion p)
		{
			PromotionUsageCollection reports = ReportManager.GetPromotionUsage(p.Id);

			GridReports.DataSource = reports;
			GridReports.DataBind();
		}

		#endregion

		#region Save to database

		protected void ButSave_Click(Object sender, EventArgs e)
		{
			Page.Validate();

			if (!Page.IsValid)
			{
				return;
			}

			Promotion p = new Promotion(Convert.ToInt32(TxtPromotionId.Text));

			try
			{
				p.Title = TxtPromotionTitle.Text.Trim();
				p.Discount = Convert.ToDouble(TxtPromotionDiscount.Text);
				p.Products = Convert.ToInt32(TxtPromotionProducts.Text);
				p.Combos = Convert.ToInt32(TxtPromotionCombos.Text);
				p.IsPercent = ChkPromotionIsPercent.Checked;
				p.Automatic = ChkPromotionAutomatic.Checked;
				p.Active = ChkPromotionActive.Checked;

				if (CboStore.SelectedValue == "0")
				{
					p.AllStores = true;
				}
				else
				{
					p.StoreId = Convert.ToInt32(CboStore.SelectedValue);
				}

				try
				{
					DataAccess.Promotion_Upsert(p);

					TxtPromotionId.Text = p.Id.ToString();

					ClientScript.RegisterStartupScript(typeof(String), "PromotionSaved", "YgluTopAlert('" + Messages.ConfirmationDataSaved + "', true);", true);

					PanGrid.Visible = true;
				}
				catch (Exception ex)
				{
					Master.SetError(ex, Messages.ErrorCouldNotSaveData);
				}
			}
			catch (Exception ex)
			{
				Master.SetError(ex, Messages.ErrorInvalidForm);
			}
		}

		protected void ButCancel_Click(Object sender, EventArgs e)
		{
			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion
	}
}